//
//  SEMineViewController.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/4.
//

import UIKit

class SEMineViewController: SEBaseViewController {

    var viewModel = SEMineViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        requestUserInfo()
    }

    override func setupNavBar() {
        super.setupNavBar()
        navigationController?.setNavigationBarHidden(true, animated: true)
    }

    override func setupUI() {
        super.setupUI()

        viewModel.updateData = { status in
            switch status {
            case .success:
                self.headerView.updateData(self.viewModel.userInfoViewModel)
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestUserInfo)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        view.addSubview(headerView)
        view.addSubview(collectionView)
        view.addSubview(helpPhoneLabel)

        headerView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(150 + UIScreen.statusBarHeight)
        }

        collectionView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(headerView.snp.bottom).offset(10)
            make.height.equalTo(230)
        }

        helpPhoneLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
//            make.top.equalTo(collectionView.snp.bottom).offset(50)
            make.bottom.equalTo(-UIScreen.bottomSafeHeight - 45)
        }
    }

    lazy var headerView: SEMineHeaderView = {
        let view = SEMineHeaderView()
        view.tapHandler = {
            self.pushToUserInfo()
        }
        return view
    }()

    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.itemSize = CGSize(width: (UIScreen.screenWidth - 30) / 3.0, height: 115)
        let collectionView = UICollectionView(frame: CGRect(), collectionViewLayout: flowLayout)
        collectionView.backgroundColor = UIColor.white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UINib(nibName: SEMineEntranceCvCell.className, bundle: nil), forCellWithReuseIdentifier: SEMineEntranceCvCell.reuseId)
        return collectionView
    }()

    lazy var helpPhoneLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.text = "客户服务热线：4001877888"
        label.font = UIFont.regular(14)
        label.textColor = UIColor(hex: "#CCCCCC")
        return label
    }()

}

// MARK: - UICollectionViewDataSource, UICollectionViewDelegate

extension SEMineViewController: UICollectionViewDataSource, UICollectionViewDelegate {

    // MARK: - UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.entranceArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEMineEntranceCvCell.reuseId, for: indexPath) as! SEMineEntranceCvCell
        cell.updateCell(model: viewModel.entranceArray[indexPath.item])
        return cell
    }

    // MARK: - UICollectionViewDelegate

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        SELog(indexPath.item)
        switch indexPath.item {
        case 0: pushToInquirySetting()
        case 1: pushToPrescriptionTemplateList()
        case 2: pushToNameCard()
//        case 3: SEHUD.show(message: "意见反馈")
        case 3: pushToSetting()
        default: break
        }
    }
}

// MARK: - Navi Push

extension SEMineViewController {
    private func pushToUserInfo() {
        let vc = SEUserInfoViewController(type: .mine)
        navigationController?.pushViewController(vc, animated: true)
    }

    private func pushToInquirySetting() {
        guard viewModel.userInfoViewModel.model.isAuthenticate == .pass else {
            SEHUD.show(in: nil, message: "请先完成医师认证", position: .center)
            return
        }
        let vc = SEInquirySettingViewController(with: viewModel.userInfoViewModel.model)
        navigationController?.pushViewController(vc, animated: true)
    }

    private func pushToNameCard() {
        let vc = SENameCardViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

    private func pushToSetting() {
        let vc = SESettingViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    /// push 处方模版 viewcontroller
    private func pushToPrescriptionTemplateList() {
        let vc = SEPrescriptionTemplateListController()
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - APIRequest

extension SEMineViewController {
    @objc func requestUserInfo() {
        viewModel.requestUserInfo()
    }
}
